package com.hivetech.controller;

import com.hivetech.models.Report;
import com.hivetech.repositories.ReportRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;


@Controller
public class ReportController {
    @Autowired
    public ReportRepository reportRepository;

    @ResponseBody
    @RequestMapping(value = "/report", method = RequestMethod.POST)
    public ResponseEntity setReport(@RequestBody Report report) {
        if (report.date == null || report.date.equals("")) report.setDate();
        if (reportRepository.findByNameAndDate(report.name, report.date) != null) {
            Report found = reportRepository.findByNameAndDate(report.name, report.date);
            String date = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
            if (date.compareTo(report.date) != 0) return new ResponseEntity<>(HttpStatus.FORBIDDEN);
            found.score = report.score;
            found.works = report.works;
            found.plan = report.plan;
            found.planPlay = report.planPlay;
            found.playTime = report.playTime;
            found.planWork = report.planWork;
            found.workTime = report.workTime;
            return new ResponseEntity<>(reportRepository.save(found), HttpStatus.OK);
        }
        if (report.date == null ) report.setDate();
        return new ResponseEntity<>(reportRepository.insert(report), HttpStatus.OK);
    }

    @ResponseBody
    @RequestMapping("/report")
    public ResponseEntity getReport(@RequestParam(name = "name", defaultValue = "/*") String name,
                                    @RequestParam(name = "data", defaultValue = "/*") String date,
                                    @PageableDefault(size = 6, sort = "date", direction = Sort.Direction.DESC) Pageable pageable) {
        if (name.equals("/*") || name.trim().equals("")) {
            if (date.equals("/*") || date.trim().equals("")) {
                return new ResponseEntity<>(reportRepository.findAll(pageable), HttpStatus.OK);
            } else {
                return new ResponseEntity<>(reportRepository.findAllByDate(date), HttpStatus.OK);
            }
        }
        System.out.println(name);
        return new ResponseEntity<>(reportRepository.findByNameOrDate(name, date, pageable), HttpStatus.OK);
    }

    @RequestMapping("/hello")
    public String sendNote(Model model,
                           HttpSession httpSession) {
        System.out.println("hello");
        return "change.html";
    }

    @RequestMapping("/this")
    public String showNotes(HttpSession httpSession) {
        return "board";
    }
}
